<template>
<div>
  <el-row class="add-button">
  <el-button type="primary" @click="dialogFormVisible = true" icon="el-icon-circle-plus">新增</el-button>
  <!-- <el-button type="danger" icon="el-icon-delete-solid">移除</el-button> -->
  </el-row>
  <el-dialog title="新增用户信息" :visible.sync="dialogFormVisible"  :close-on-click-modal="false"
    @close="clear">
  <el-form :model="loginForm" :rules="rules">
    <el-form-item label="账号" :label-width="formLabelWidth" prop="username">
      <el-input v-model="loginForm.username" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
      <el-input v-model="loginForm.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="真实姓名" :label-width="formLabelWidth">
      <el-input v-model="loginForm.name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" :label-width="formLabelWidth">
      <el-input v-model="loginForm.phone" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" :label-width="formLabelWidth">
      <el-input v-model="loginForm.email" autocomplete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" v-on:click="register">确 定</el-button>
  </div>
</el-dialog>
</div>
</template>

<script>
    export default {
        name: 'BulkRegistration',
      data () {
        return {
          dialogFormVisible: false,
          dialogTableVisible: false,
          rules: {
            username: [{required: true, message: '用户名不能为空', trigger: 'blur'}],
            password: [{required: true, message: '密码不能为空', trigger: 'blur'}],
          },
          loginForm: {
            username: '',
            password: '',
            name: '',
            phone: '',
            email: ''
          },
          formLabelWidth:'120px'
        }
      },
      methods: {
        clear () {
          this.loginForm = {
            username: '',
            password: '',
            name: '',
            phone: '',
            email: ''
          }
        },
        register () {
          this.$loading({
                  lock: true,
                  text: "Loading",
                  spinner: "el-icon-loading",
                  background: "rgba(0, 0, 0, 0.7)"
                })
          this.$axios
            .post('/register', {
              username: this.loginForm.username,
              password: this.loginForm.password,
              name: this.loginForm.name,
              phone: this.loginForm.phone,
              email: this.loginForm.email
            })
            .then(resp => {
              if (resp.data.code === 200) {
                this.$loading({
                  lock: false,
                  text: "Loading",
                  spinner: "el-icon-loading",
                  background: "rgba(0, 0, 0, 0.7)"
                }).close();
                this.$message.success('注册成功')
                this.clear()
                this.$emit('onSubmit')
              } else {
                this.$loading({
                  lock: false,
                  text: "Loading",
                  spinner: "el-icon-loading",
                  background: "rgba(0, 0, 0, 0.7)"
                }).close();
                this.$message.warning(resp.data.message)
              }
            })
            .catch(failResponse => {
              this.$loading({
                  lock: false,
                  text: "Loading",
                  spinner: "el-icon-loading",
                  background: "rgba(0, 0, 0, 0.7)"
                }).close();
              console.log(JSON.stringify(failResponse))
            })
        }
      }
    }
</script>

<style scoped>
  .add-button {
    margin-right: 90%;
    margin-top: 2%;
  }
</style>
